package com.concerneyes.glasses;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.glasses.adapter.GlassesItemAdapter;
import com.concerneyes.glasses.model.GlassesDetailModel;
import com.concerneyes.glasses.model.GlassesItemModel;
import com.concerneyes.glasses.model.GlassesList;
import com.concerneyes.glasses.task.GlassesImpl;
import com.concerneyes.glasses.task.GlassesListener;

import java.util.Arrays;
import java.util.List;

import common.AbsBaseView;
import http.JsonMap;
import util.ActivityUtil;

/**
 * 分类后产品列表
 */

public class GlassesInfoView extends AbsBaseView implements AdapterView.OnItemClickListener, OnClickListener, GlassesListener, SwipeRefreshLayout.OnRefreshListener, AbsListView.OnScrollListener {
    private GridView gv_fm_glasses;
    private GlassesItemAdapter itemAdapter;
    private GlassesImpl impl;
    private SwipeRefreshLayout swipeLayout;
    private int categoryId;
    private TextView tv_load_more;
    private int curPage = 1;
    private Activity activity;

    public GlassesInfoView(Context context, int categoryId, Activity activity) {
        super(context);
        this.activity = activity;
        this.categoryId = categoryId;
        initView();
    }

    private void initView() {
        gv_fm_glasses = (GridView) findViewById(R.id.gv_glasses);
        tv_load_more = (TextView) findViewById(R.id.tv_load_more);

        itemAdapter = new GlassesItemAdapter(activity);
        gv_fm_glasses.setAdapter(itemAdapter);
        gv_fm_glasses.setOnItemClickListener(this);
        impl = new GlassesImpl(this);
        impl.glassesList(categoryId, curPage, CEApp.LIST_PAGENUM);
        gv_fm_glasses.setOnScrollListener(this);
        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeResources(android.R.color.holo_orange_dark, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
    }


    @Override
    public int layoutResId() {
        return R.layout.view_price_glasses;
    }

    @Override
    public void loadData(Object data) {

    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onCategoryListSuccess(List<JsonMap> datas) {

    }

    @Override
    public void onCategoryListFail() {

    }

    @Override
    public void onGlassesListSuccess(GlassesItemModel datas) {
        tv_load_more.setVisibility(View.GONE);
        swipeLayout.setRefreshing(false);
        itemAdapter.addDatas(datas);
        gv_fm_glasses.setAdapter(itemAdapter);
    }

    @Override
    public void onGlassesListFail() {

    }

    @Override
    public void onGlassesInfoSuccess(GlassesDetailModel datas) {

    }

    @Override
    public void onGlassesInfoFail() {

    }


    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                swipeLayout.setRefreshing(true);
                impl.glassesList(categoryId, curPage, CEApp.LIST_PAGENUM);
                itemAdapter.clearList();
            }
        }, 3000);

    }

    boolean isLastRow = false;

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            if (isLastRow) {
                tv_load_more.setVisibility(View.VISIBLE);
                impl.glassesList(categoryId, curPage + 1, CEApp.LIST_PAGENUM);
                isLastRow = false;
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //判断是否滚到最后一行
        if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
            isLastRow = true;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        GlassesList item = itemAdapter.getItem(position);
        String productNo = item.getProductNo();
        String frameColors = item.getFrameColors();
        String replace = frameColors.replace(" ", "");
        List<String> str = Arrays.asList(replace.split(","));
        Bundle bundle = new Bundle();
        bundle.putString("productNo", productNo);
        bundle.putString("frameColors", str.get(0));
        ActivityUtil.setIntent(getContext(), GlassesDetail.class, bundle);
    }
}
